import {Component, OnInit} from '@angular/core';
import {BrowserUtils} from '../lunhui-web-front-utils/BrowserUtils';
import {HttpQueryParamsDto} from '../lunhui-web-front-dto/HttpQueryParamsDto';
import {environment} from '../../environments/environment';
import {HttpMethodEnum} from '../lunhui-web-front-utils/HttpMethodEnum';
import {HttpUtil} from '../lunhui-web-front-utils/HttpUtil';
import {ResponseBaseDto} from '../lunhui-web-front-dto/ResponseBaseDto';
import {Rose} from '../lunhui-web-front-dto/Rose';
import * as $ from 'jquery';

@Component({
    selector: 'app-lunhui-web-front-roseselectview',
    templateUrl: './lunhui-web-front-roseselectview.component.html',
    styleUrls: ['./lunhui-web-front-roseselectview.component.scss']
})
export class LunhuiWebFrontRoseselectviewComponent implements OnInit {

    public roseList: Array<Rose>;
    private roseName: string;

    constructor() {
    }

    ngOnInit() {
        const result: boolean = BrowserUtils.checkToken();

        if (!result) {
            BrowserUtils.backToTarget('login');
            return;
        }

        this.getRoseList();
    }

    // 获取角色
    private async getRoseList(): Promise<void> {

        try {
            const cookie = JSON.parse(BrowserUtils.getCookie('token'));

            const httpQueryParamsDto: HttpQueryParamsDto = new HttpQueryParamsDto();
            httpQueryParamsDto.url = `${environment.host}user/roseNameListInfo?userId=`;
            httpQueryParamsDto.url += `${cookie.userId}&token=${cookie.token}`;
            httpQueryParamsDto.method = HttpMethodEnum.GET;

            const resultData = await HttpUtil.query(httpQueryParamsDto) as ResponseBaseDto;

            if (resultData.success && resultData.result !== null) {
                this.roseList = resultData.result;
            }  else {
                HttpUtil.queryError(resultData.errorMessage);

                BrowserUtils.deleteCookie('token');
                BrowserUtils.deleteCookie('roseId');

                const timer: number = setTimeout(() => {
                    clearTimeout(timer);
                    BrowserUtils.backToTarget('login');
                }, 2000);
            }
        } catch (e) {
            console.error(e);
        }

    }

    // 创建角色
    private async createRose(): Promise<void> {
        if (!this.roseName) {
            HttpUtil.queryError('角色名不能为空');
            return;
        }
        const cookie = JSON.parse(BrowserUtils.getCookie('token'));

        const httpQueryParamsDto: HttpQueryParamsDto = new HttpQueryParamsDto();
        httpQueryParamsDto.url = `${environment.host}user/createRose?userId=`;
        httpQueryParamsDto.url += `${cookie.userId}&token=${cookie.token}&`;
        httpQueryParamsDto.url += `&roseName=${this.roseName}`;
        httpQueryParamsDto.method = HttpMethodEnum.POST;

        const resultData = await HttpUtil.query(httpQueryParamsDto) as ResponseBaseDto;

        if (resultData.success) {
            HttpUtil.queryError('创建角色成功');
            setTimeout(() => location.reload(), 2500);
        } else {
            HttpUtil.queryError(resultData.errorMessage);
        }
    }

    private showCreateRoseView(): void {
        $('.createRoseView').fadeIn();
    }

    private selectRose(roseId: number): void {
        if (typeof roseId === 'number') {
            BrowserUtils.setCookie('roseId', roseId.toString());
            LunhuiWebFrontRoseselectviewComponent.roseLogin();
        } else {
            HttpUtil.queryError('roseId is null');
        }
    }

    private static async roseLogin(): Promise<void> {

        const cookie = JSON.parse(BrowserUtils.getCookie('token'));
        const roseId = JSON.parse(BrowserUtils.getCookie('roseId'));

        const httpQueryParamsDto: HttpQueryParamsDto = new HttpQueryParamsDto();
        httpQueryParamsDto.url = `${environment.host}rose/login?userId=`;
        httpQueryParamsDto.url += `${cookie.userId}&token=${cookie.token}&`;
        httpQueryParamsDto.url += `roseId=${roseId}`;
        httpQueryParamsDto.method = HttpMethodEnum.POST;

        const resultData = await HttpUtil.query(httpQueryParamsDto) as ResponseBaseDto;

        if (resultData.success) {
            console.log(resultData);
            BrowserUtils.backToTarget('home');
        } else {
            HttpUtil.queryError(resultData.errorMessage);

            BrowserUtils.deleteCookie('token');
            BrowserUtils.deleteCookie('roseId');

            const timer: number = setTimeout(() => {
                clearTimeout(timer);
                BrowserUtils.backToTarget('login');
            }, 2000);
        }

    }
}
